package com.biglead.mini.controller;


import com.biglead.common.pojo.CommonResult;
import com.biglead.mini.pojo.UmsMemberReceiveAddress;
import com.biglead.mini.service.UmsMemberReceiveAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址管理Controller
 */
@Controller
@Api(tags = "UmsMemberReceiveAddressController")
@RequestMapping("/member/address")
public class AddressController {
    @Autowired
    private UmsMemberReceiveAddressService umsMemberReceiveAddressService;

    @ApiOperation("添加收货地址")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestHeader Long userId,@RequestBody UmsMemberReceiveAddress address) {
        address.setMemberId(userId);
        boolean count = umsMemberReceiveAddressService.add(address);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除收货地址")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean count = umsMemberReceiveAddressService.deleteFromId(id);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改收货地址")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody UmsMemberReceiveAddress address) {
        boolean count = umsMemberReceiveAddressService.updateFromId(id, address);
        if (count ) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("显示所有收货地址")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsMemberReceiveAddress>> list() {
        List<UmsMemberReceiveAddress> addressList = umsMemberReceiveAddressService.getAllList();
        return CommonResult.success(addressList);
    }

    @ApiOperation("获取收货地址详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMemberReceiveAddress> getItem(@PathVariable Long id) {
        UmsMemberReceiveAddress address = umsMemberReceiveAddressService.getItem(id);
        return CommonResult.success(address);
    }
}
